<template>
  <div class="app-container">
    <el-row v-if="optType != 'view'" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:wm:issueheader:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="issuelineList">
      <el-table-column label="产品物料编码" width="150px" align="center" prop="itemCode" :show-overflow-tooltip="true"/>
      <el-table-column label="产品物料名称" width="120px" align="center" prop="itemName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" width="150px" align="center" prop="specification" :show-overflow-tooltip="true"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" :show-overflow-tooltip="true"/>
      <el-table-column
        v-for="col in columnsConfig"
        :key="col.prop"
        :label="col.label"
        :prop="col.prop"
        :width="col.width"
        :align="col.align"
        v-if="col.show">
      </el-table-column>
      <el-table-column label="批次号" align="center" prop="batchCode" width="180px" :show-overflow-tooltip="true"/>
      <el-table-column label="仓库名称" align="center" prop="warehouseName" width="120px" :show-overflow-tooltip="true"/>
      <el-table-column label="库区名称" align="center" prop="locationName" width="120px" :show-overflow-tooltip="true"/>
      <el-table-column label="库位名称" align="center" prop="areaName" width="120px" :show-overflow-tooltip="true"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.price * scope.row.quantityIssued }}
        </template>
      </el-table-column>
      <el-table-column label="领料数量" align="center" prop="quantityIssued" fixed="right">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.quantityIssued }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <ProtaskSelect ref="taskSelect" :otherStatus="otherStatus" @onSelected="onTaskSelected"></ProtaskSelect>
  </div>
</template>

<script>
import { listIssueline, mergeIssueline } from "@/api/mes/wm/issueline";
import StockSelect from "@/components/stockSelect/single.vue"
import ProtaskSelect from "@/components/TaskSelect/taskSelectMulti.vue";
import { getColumnsConfigKey } from "@/api/system/columnsconfig";

export default {
  name: "IssueAllLine",
  components: {ProtaskSelect, StockSelect},
  props: {
    otherStatus: null,
    optType: null,
    issueId: null,
  },

  data() {
    return {
      columnsConfig: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产领料单行表格数据
      issuelineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        issueId: this.issueId,
        workorderId: this.workorderId,
        itemId: null,
        itemCode: this.itemCode,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantityIssued: null,
        batchCode: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemCode: [
          {required: true, message: "产品物料不能为空", trigger: "blur"}
        ],
        quantityIssued: [
          {required: true, message: "领料数量不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getItemBase();
    this.getList();
  },
  methods: {
    /** 查询生产领料单行列表 */
    getList() {
      this.loading = true;
      listIssueline(this.queryParams).then(response => {
        this.issuelineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lineId: null,
        issueId: this.issueId,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantityIssued: null,
        batchCode: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleAdd() {
      this.$refs.taskSelect.showFlag = true;
      this.$refs.taskSelect.getList();
    },
    onTaskSelected(rows) {
      if (rows != undefined && rows != null) {
        const taskIds = rows.map(item => item.taskId);
        const data = {
          issueId: this.issueId,   // 假设 issueId 存储在 this.issueId 中
          taskIds: taskIds
        };
        mergeIssueline(data).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    getItemBase() {
      getColumnsConfigKey('MdItem_Base').then(res => {
        this.columnsConfig = JSON.parse(res.data);
      })
    },
  }
}
</script>
